package org.hackystat.ui.chart.jfc.chartresource.dataset;

import java.awt.Color;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.TreeMap;

/**
 * Encapsulates XY series data.
 * 
 * @author Pavel Senin.
 * 
 */
public class TelemetryXYSeries {

  /** holds the series data. */
  private TreeMap<Double, Double> seriesData;
  /** holds the series color for drawing. */
  private Color color;

  /**
   * Formal constructor, creates empty storage.
   */
  public TelemetryXYSeries() {
    this.seriesData = new TreeMap<Double, Double>();
  }

  /**
   * Sets the color of the time series.
   * 
   * @param col color to set.
   */
  public void setColor(Color col) {
    this.color = col;
  }

  /**
   * Reports the time-series color.
   * 
   * @return time series color.
   */
  public Color getColor() {
    return this.color;
  }

  /**
   * Adds a point into the series.
   * 
   * @param x the point X coordinate.
   * @param y the point Y coordinate.
   * @throws TelemetryXYSeriesException when some data got overridden.
   */
  public void add(Double x, Double y) throws TelemetryXYSeriesException {
    if (this.seriesData.containsKey(x)) {
      throw new TelemetryXYSeriesException("Overriding the series value: (" + x + ","
          + this.seriesData.get(x) + ") with (" + x + "," + y);
    }
    else {
      this.seriesData.put(x, y);
    }
  }

  /**
   * Reports the series data.
   * 
   * @return the series data. Map key values are X coordinates, matching values are Y coordinates.
   */
  public TreeMap<Double, Double> getSeriesData() {
    return this.seriesData;
  }

  /**
   * Sets the new data series.
   * 
   * @param data the data to set.
   */
  public void setData(ArrayList<Point2D.Double> data) {
    this.seriesData = new TreeMap<Double, Double>();
    for (Point2D.Double point : data) {
      this.seriesData.put(point.getX(), point.getY());
    }
  }

}
